﻿Public Class clsCharacter

    Private m_CharacterID As Integer
    Private m_StartOffset As Integer
    Public ReadOnly Property StartOffset() As Integer
        Get
            Return m_StartOffset
        End Get
    End Property

    Private m_Index As Integer
    Public ReadOnly Property Index() As Integer
        Get
            Return m_Index
        End Get
    End Property

    Private m_Status As Integer
    Private m_Strength1 As Integer
    Private m_Strength2 As Integer
    Private m_Intelligence As Integer
    Private m_Wisdom As Integer
    Private m_Dexterity As Integer
    Private m_Constitution As Integer
    Private m_Charisma As Integer
    Private m_ArmorClass As Integer

    Private m_Race As Integer
    Private m_Class As Integer
    Private m_Alignment As Integer
    Private m_Image As Integer

    Private m_Experience1 As Integer
    Private m_Experience2 As Integer
    Private m_Experience3 As Integer
    Private m_Level1 As Integer
    Private m_Level2 As Integer
    Private m_Level3 As Integer

    Private m_HPMax As Integer
    Public ReadOnly Property HPMax() As Integer
        Get
            Return m_HPMax
        End Get
    End Property

    Private m_HPActual As Integer
    Private m_Food As Integer

    Private m_Name As String
    Public ReadOnly Property Name() As String
        Get
            Return m_Name
        End Get
    End Property

    Public Sub New(ByVal CharacterID As Integer, ByVal StartOffset As Integer)
        m_CharacterID = CharacterID
        m_StartOffset = StartOffset
    End Sub

    Public Function Load(ByVal ByteData() As Byte) As Boolean

        Dim HexString As New System.Text.StringBuilder

        Try

            m_Index = ByteData(m_StartOffset)
            m_Status = ByteData(m_StartOffset + 1)

            For i As Integer = (m_StartOffset + 2) To (m_StartOffset + 11)
                If ByteData(i) = "0" Then Exit For
                HexString.Append(Chr(ByteData(i)))
            Next

            m_Name = HexString.ToString.Trim

            m_Strength1 = ByteData(m_StartOffset + 13)
            m_Strength2 = ByteData(m_StartOffset + 15)
            m_Intelligence = ByteData(m_StartOffset + 17)
            m_Wisdom = ByteData(m_StartOffset + 19)
            m_Dexterity = ByteData(m_StartOffset + 21)
            m_Constitution = ByteData(m_StartOffset + 23)
            m_Charisma = ByteData(m_StartOffset + 25)

            Dim sHP As String = ByteData(m_StartOffset + 27).ToString("x")

            If sHP = "f6" Then
                m_HPActual = 0
                m_Status = 7
                m_Index = 44
            Else
                m_HPActual = ByteData(m_StartOffset + 27)
            End If

            m_HPMax = ByteData(m_StartOffset + 28)

            m_ArmorClass = ByteData(m_StartOffset + 29)

            m_Race = ByteData(m_StartOffset + 31)
            m_Class = ByteData(m_StartOffset + 32)
            m_Alignment = ByteData(m_StartOffset + 33)
            m_Image = ByteData(m_StartOffset + 34)
            m_Food = ByteData(m_StartOffset + 35)

            m_Level1 = ByteData(m_StartOffset + 36)
            m_Level2 = ByteData(m_StartOffset + 37)
            m_Level3 = ByteData(m_StartOffset + 38)

            Dim hexExp As String = ByteData(m_StartOffset + 40).ToString("x") & ByteData(m_StartOffset + 39).ToString("x")
            m_Experience1 = Integer.Parse(hexExp, Globalization.NumberStyles.HexNumber)

            hexExp = ByteData(m_StartOffset + 44).ToString("x") & ByteData(m_StartOffset + 43).ToString("x")
            m_Experience2 = Integer.Parse(hexExp, Globalization.NumberStyles.HexNumber)

            hexExp = ByteData(m_StartOffset + 48).ToString("x") & ByteData(m_StartOffset + 47).ToString("x")
            m_Experience3 = Integer.Parse(hexExp, Globalization.NumberStyles.HexNumber)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Sub CharacterImage(ByVal Index As Integer, ByVal frm As frmMain)

        Select Case Index
            Case 0
                frm.PictureBox1.Image = My.Resources.char0
            Case 1
                frm.PictureBox1.Image = My.Resources.char1
            Case 2
                frm.PictureBox1.Image = My.Resources.char2
            Case 3
                frm.PictureBox1.Image = My.Resources.char3
            Case 4
                frm.PictureBox1.Image = My.Resources.char4
            Case 5
                frm.PictureBox1.Image = My.Resources.char5
            Case 6
                frm.PictureBox1.Image = My.Resources.char6
            Case 7
                frm.PictureBox1.Image = My.Resources.char7
            Case 8
                frm.PictureBox1.Image = My.Resources.char8
            Case 9
                frm.PictureBox1.Image = My.Resources.char9
            Case 10
                frm.PictureBox1.Image = My.Resources.char10
            Case 11
                frm.PictureBox1.Image = My.Resources.char11
            Case 12
                frm.PictureBox1.Image = My.Resources.char12
            Case 13
                frm.PictureBox1.Image = My.Resources.char13
            Case 14
                frm.PictureBox1.Image = My.Resources.char14
            Case 15
                frm.PictureBox1.Image = My.Resources.char15
            Case 16
                frm.PictureBox1.Image = My.Resources.char16
            Case 17
                frm.PictureBox1.Image = My.Resources.char17
            Case 18
                frm.PictureBox1.Image = My.Resources.char18
            Case 19
                frm.PictureBox1.Image = My.Resources.char19
            Case 20
                frm.PictureBox1.Image = My.Resources.char20
            Case 21
                frm.PictureBox1.Image = My.Resources.char21
            Case 22
                frm.PictureBox1.Image = My.Resources.char22
            Case 23
                frm.PictureBox1.Image = My.Resources.char23
            Case 24
                frm.PictureBox1.Image = My.Resources.char24
            Case 25
                frm.PictureBox1.Image = My.Resources.char25
            Case 26
                frm.PictureBox1.Image = My.Resources.char26
            Case 27
                frm.PictureBox1.Image = My.Resources.char27
            Case 28
                frm.PictureBox1.Image = My.Resources.char28
            Case 29
                frm.PictureBox1.Image = My.Resources.char29
            Case 30
                frm.PictureBox1.Image = My.Resources.char30
            Case 31
                frm.PictureBox1.Image = My.Resources.char31
            Case 32
                frm.PictureBox1.Image = My.Resources.char32
            Case 33
                frm.PictureBox1.Image = My.Resources.char33
            Case 34
                frm.PictureBox1.Image = My.Resources.char34
            Case 35
                frm.PictureBox1.Image = My.Resources.char35
            Case 36
                frm.PictureBox1.Image = My.Resources.char36
            Case 37
                frm.PictureBox1.Image = My.Resources.char37
            Case 38
                frm.PictureBox1.Image = My.Resources.char38
            Case 39
                frm.PictureBox1.Image = My.Resources.char39
            Case 40
                frm.PictureBox1.Image = My.Resources.char40
            Case 41
                frm.PictureBox1.Image = My.Resources.char41
            Case 42
                frm.PictureBox1.Image = My.Resources.char42
            Case 43
                frm.PictureBox1.Image = My.Resources.char43
            Case 44
                frm.PictureBox1.Image = My.Resources.char44
            Case Else
                frm.PictureBox1.Image = Nothing
        End Select
    End Sub

    Public Sub Show(ByVal frm As frmMain)
        With frm
            .txtName.Text = m_Name
            .cboRace.SelectedIndex = m_Race
            .cboClass.SelectedIndex = m_Class
            .cboAlignment.SelectedIndex = m_Alignment
            .txtStrength1.Text = m_Strength1

            .cboStatus.Text = [Enum].GetName(GetType(CharacterStatus), m_Status)

            If m_Class > 5 Then
                .txtStrength2.Enabled = True
                .txtStrength2.Text = m_Strength2
            Else
                .txtStrength2.Enabled = False
                .txtStrength2.Text = String.Empty
            End If
            .txtIntelligence.Text = m_Intelligence
            .txtWisdom.Text = m_Wisdom
            .txtDexterity.Text = m_Dexterity
            .txtConstitution.Text = m_Constitution
            .txtCharisma.Text = m_Charisma
            .txtArmorClass.Text = m_ArmorClass

            Dim sClass() As String = .cboClass.Text.Split("/")

            .lblClass2.Visible = False
            .lblClassExplbl2.Visible = False
            .txtClassExp2.Visible = False
            .lblClassLevel2.Visible = False

            .lblClass3.Visible = False
            .lblClassExplbl3.Visible = False
            .txtClassExp3.Visible = False
            .lblClassLevel3.Visible = False

            .lblClass1.Text = String.Format("{0}:", sClass(0))
            .txtClassExp1.Text = m_Experience1
            .lblClassLevel1.Text = String.Format("Level: {0}", m_Level1)

            Select Case sClass.Count
                Case 2
                    .lblClass2.Visible = True
                    .lblClassExplbl2.Visible = True
                    .txtClassExp2.Visible = True
                    .lblClassLevel2.Visible = True

                    .lblClass2.Text = String.Format("{0}:", sClass(1))
                    .txtClassExp2.Text = m_Experience2
                    .lblClassLevel2.Text = String.Format("Level: {0}", m_Level2)

                Case 3
                    .lblClass2.Visible = True
                    .lblClassExplbl2.Visible = True
                    .txtClassExp2.Visible = True
                    .lblClassLevel2.Visible = True

                    .lblClass2.Text = String.Format("{0}:", sClass(1))
                    .txtClassExp2.Text = m_Experience2
                    .lblClassLevel2.Text = String.Format("Level: {0}", m_Level2)

                    .lblClass3.Visible = True
                    .lblClassExplbl3.Visible = True
                    .txtClassExp3.Visible = True
                    .lblClassLevel3.Visible = True

                    .lblClass3.Text = String.Format("{0}:", sClass(2))
                    .txtClassExp3.Text = m_Experience3
                    .lblClassLevel3.Text = String.Format("Level: {0}", m_Level3)
            End Select

            .pgHP.Maximum = m_HPMax
            .pgHP.Value = m_HPActual

            .pgFood.Value = m_Food

            Call CharacterImage(m_Image, frm)
        End With
    End Sub
End Class
